/******************************************************************************
** 
** File name:	utest_65.c
**	
** Description:	RTOS Ver.3 Unit Tests. RTOS System Timer subsystem.
**
**				This unit test is to check RTOS_Delay() timer services.
**
** RTOS API Services to be checked:
**
**		- RTOS_Delay()
**
** Notes:	PASSED
**
*******************************************************************************
**
** Change history:
**	
** 10/01/2012 -AK- 	Test created
**
******************************************************************************/
#include <avr/io.h>
#include <avr/interrupt.h>

#ifndef UTEST_65
#	error "UTEST_65 must be defined as -D$(Configuration) compiler option"
#endif

#include "UTEST_RES.h"

/******************************************************************************
**	UNIT TEST INTERRUPT FUNCTION[s]
******************************************************************************/
/*
** User' Timer Handler
*/
RTOS_ISR_FUNC(TIMER_1_Handler)
{
	RTOS_DebugPrint("[$1] timer interrupt at %d",RTOS_GetTimeISR());
	RTOS_DebugLogDump();
	
	RTOS_DebugPrint("[$1] resuming task#3");
	RTOS_TaskResumeISR(&tsk3PID);
	RTOS_DebugLogDump();

	//RTOS_DebugPrint("[$1] resuming task#2");
	//RTOS_TaskResumeISR(&tsk2PID);
	//RTOS_DebugLogDump();

	RTOS_DebugPrint("[$1] resuming task#1");
	RTOS_TaskResumeISR(&tsk1PID);
	RTOS_DebugLogDump();

	RTOS_DebugPrint("[$1] leaving handler at %d",RTOS_GetTimeISR());
}

/******************************************************************************
**	UNIT TEST TASK FUNCTIONs
******************************************************************************/
/*
** RTOS Task #0 - Stack checking task
*/
RTOS_TASK_FUNC(tsk0)
{
	while (1)
	{	
		RTOS_DebugPrint("[T0] Checking stacks consistency");
		
		RTOS_StackCheck(tsk0);
		RTOS_StackCheck(tsk1);
		RTOS_StackCheck(tsk2);
		RTOS_StackCheck(tsk3);
		
		RTOS_TaskSuspend(&tsk0PID);
		
	}
}

/*
** RTOS Task #1
*/
RTOS_TASK_FUNC(tsk1)
{
	RTOS_EST_t	tst;

	RTOS_DebugPrint("\t[T1] starting at %d",RTOS_GetTime());
	
	while(1)
	{
	
		RTOS_DebugPrint("\t[T1] wakes up at %d",RTOS_GetTime());
		RTOS_TaskResume(&tsk0PID);		// activate stack control check
	
		tst = RTOS_TestTimer(&tmr2);
		switch ( tst )
		{
			case EVT_PEND:
				RTOS_DebugPrint("\t[T1] timer #2 is PEND.");
				break;
			case EVT_DONE:
				RTOS_DebugPrint("\t[T1] timer #2 is DONE.");
				break;
			case EVT_WAIT:
				RTOS_DebugPrint("\t[T1] timer #2 is WAIT.");
				break;
			default:
				RTOS_DebugPrint("\t[T1] timer #2 is UNKNOWN.");
				break;
		}
		
		RTOS_DebugPrint("\t[T1] suspending at %d",RTOS_GetTime());
		RTOS_TaskSuspend(&tsk1PID);		
	}
}

/*
** RTOS Task #2 
*/
RTOS_TASK_FUNC(tsk2)
{
	RTOS_EST_t tst;

	RTOS_DebugPrint("\t\t[T2] starting at %d",RTOS_GetTime());
	
	while(1)
	{		
		RTOS_DebugPrint("\t\t[T2] wake up at %d",RTOS_GetTime());
		
		RTOS_TaskResume(&tsk0PID);		// activate stack control check
		
		RTOS_DebugPrint("\t\t[T2] blocks on delay (timer #2) at %d",RTOS_GetTime());
		RTOS_Delay(&tmr2,17);
	}
}

/*
** RTOS Task #3 
*/
RTOS_TASK_FUNC(tsk3)
{
	RTOS_EST_t tst;

	RTOS_DebugPrint("\t\t\t[T3] starting at %d", RTOS_GetTime());

	while(1)
	{
		RTOS_DebugPrint("\t\t\t[T3] wake up at %d",RTOS_GetTime());
		RTOS_TaskResume(&tsk0PID);		// activate stack control check

		tst = RTOS_TestTimer(&tmr2);
		switch ( tst )
		{
			case EVT_PEND:
				RTOS_DebugPrint("\t\t\t[T3] timer #2 is PEND.");
				break;
			case EVT_DONE:
				RTOS_DebugPrint("\t\t\t[T3] timer #2 is DONE.");
				break;
			case EVT_WAIT:
				RTOS_DebugPrint("\t\t\t[T3] timer #2 is WAIT.");
				break;
			default:
				RTOS_DebugPrint("\t\t\t[T3] timer #2 is UNKNOWN.");
				break;
		}
		
		RTOS_DebugPrint("\t\t\t[T3] suspending at %d",RTOS_GetTime());
		RTOS_TaskSuspend(&tsk3PID);		
	}
}

